# 导包
import allure
import requests



# 创建登录api测试类
class LoginApi:
    def __init__(self, ip_port="http://ihrm2-test.itheima.net"):
        self.ip_port = ip_port

    # 创建登录的函数
    @allure.step(title="登录")
    def login(self, json_data=None, path="/api/sys/login", headers=None):
        # 构造登录的url
        login_url = self.ip_port + path
        allure.attach('URL', login_url)
        # 默认数据
        if headers is None:
            headers = {"Content-Type": "application/json","Connection":"keep-alive"}
        allure.attach('headers', str(headers))
        if json_data is None:
            json_data = {
                "mobile": "13800000002",
                "password": "123456"
            }
        allure.attach('request.body', str(json_data))
        # 发送登录请求
        response = requests.post(url=login_url,
                                 json=json_data,
                                 headers=headers)

        allure.attach('response.status_code', str(response.status_code))
        allure.attach('response.body', response.text)
        return response